package com.linpq.road.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.linpq.common.core.domain.AjaxResult;
import com.linpq.common.core.service.CrudService;
        import com.linpq.common.utils.DateUtils;
import com.linpq.common.utils.StringUtils;
import com.linpq.road.domain.*;
import com.linpq.road.mapper.RdImportLogMapper;
import com.linpq.road.mapper.RdPavementStructureMapper;
import com.linpq.road.mapper.RdRoadSectionMapper;
import com.linpq.road.mapper.RdRoadStakeMapper;
import com.linpq.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linpq.common.core.text.Convert;

/**
 * 路面结构Service业务层处理
 *
 * @author linpq
 * @date 2021-07-29
 */
@Service
public class RdPavementStructureService  extends CrudService<RdPavementStructureMapper, RdPavementStructure> {

    @Autowired
    private RdRoadSectionMapper sectionMapper;
    @Autowired
    private RdPavementStructureMapper rdPavementStructureMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private RdRoadStakeMapper rdRoadStakeMapper;
    @Autowired
    private RdImportLogMapper rdImportLogMapper;

    /**
     * 查询路面结构
     *
     * @param id 路面结构ID
     * @return 路面结构
     */

    public RdPavementStructure selectRdPavementStructureById(String id) {
        return dao.get(id);
    }

    /**
     * 查询路面结构列表
     *
     * @param rdPavementStructure 路面结构
     * @return 路面结构
     */

    public List<RdPavementStructure> selectRdPavementStructureList(RdPavementStructure rdPavementStructure) {
        return dao.findList(rdPavementStructure);
    }

    /**
     * 新增路面结构
     *
     * @param rdPavementStructure 路面结构
     * @return 结果
     */

    public int insertRdPavementStructure(RdPavementStructure rdPavementStructure) {
        rdPavementStructure.setCreateTime(DateUtils.getNowDate());
        return super.save(rdPavementStructure);
    }

    /**
     * 修改路面结构
     *
     * @param rdPavementStructure 路面结构
     * @return 结果
     */

    public int updateRdPavementStructure(RdPavementStructure rdPavementStructure) {
                rdPavementStructure.setUpdateTime(DateUtils.getNowDate());
        return super.save(rdPavementStructure);
    }

    /**
     * 删除路面结构对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */

    public int deleteRdPavementStructureByIds(String ids) {
        return dao.deleteRdPavementStructureByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除路面结构信息
     *
     * @param id 路面结构ID
     * @return 结果
     */
    public int deleteRdPavementStructureById(String id) {
        return dao.delete(id);
    }

    /**
     * 初始化：
     * 1、面层结构组合
     * 2、基层结构组合
     * 3、面层厚度组合
     * 4、基层厚度组合
     *
     * @return 需要初始化信息
     */
    public Map<String,List> getNotInitInfoByRdPavementStructure(){
        Map<String, List> rdPavementStructureMap = new HashMap();
        //已初始化路转未初始化路面材料的路段列表
        List<String> stringArrayRoadList = new ArrayList<>();
        //面层结构组合
        List<String> stringArrayRoadSurfaceStructureList = new ArrayList<>();
        stringArrayRoadSurfaceStructureList.add(sysDictDataMapper.selectDictLabel("road_combinationOfSurfaceStructure","1"));
        stringArrayRoadSurfaceStructureList.add(sysDictDataMapper.selectDictLabel("road_combinationOfSurfaceStructure","2"));
        stringArrayRoadSurfaceStructureList.add(sysDictDataMapper.selectDictLabel("road_combinationOfSurfaceStructure","3"));
        //基层结构组合
        List<String> stringArrayRoadBaseStructureList = new ArrayList<>();
        stringArrayRoadBaseStructureList.add(sysDictDataMapper.selectDictLabel("road_combinationOfBaseStructure","1"));
        stringArrayRoadBaseStructureList.add(sysDictDataMapper.selectDictLabel("road_combinationOfBaseStructure","2"));
        //面层厚度组合
        List<String> stringArrayRoadSurfaceThicknessList = new ArrayList<>();
        stringArrayRoadSurfaceThicknessList.add(sysDictDataMapper.selectDictLabel("road_combinationOfSurfaceThickness","1"));
        stringArrayRoadSurfaceThicknessList.add(sysDictDataMapper.selectDictLabel("road_combinationOfSurfaceThickness","2"));
        //基层厚度组合
        List<String> stringArrayRoadBaseThicknessList = new ArrayList<>();
        stringArrayRoadBaseThicknessList.add(sysDictDataMapper.selectDictLabel("road_combinationOfBaseThickness","1"));
        stringArrayRoadBaseThicknessList.add(sysDictDataMapper.selectDictLabel("road_combinationOfBaseThickness","2"));
        //更具锁来查询当前已经初始化的路段
        List<RdRoadSection> rdRoadSections = sectionMapper.selectRdRoadSectionByLocked();
        for (RdRoadSection rdRoadSection:rdRoadSections) {
            //标志位用于判断路面材料的对象是否存在
            boolean uninitialized=false;
            //获取已经初始化的路段在路面材料表判断当前路段的路面材料是否已经初始化
            List<RdPavementStructure> rdPavementStructures = rdPavementStructureMapper.selectRdPavementStructureNameList(rdRoadSection.getId());
            //如果这个路面材料的对象存在，则不需要将她加入我们的集合
            if (rdPavementStructures.size()==0){
                uninitialized=true;
            }
            if (uninitialized){
                stringArrayRoadList.add(rdRoadSection.getSectionName());
            }
        }
        rdPavementStructureMap.put("rdRoadSections",rdRoadSections);
        rdPavementStructureMap.put("stringArrayRoadSurfaceStructureList",stringArrayRoadSurfaceStructureList);
        rdPavementStructureMap.put("stringArrayRoadBaseStructureList",stringArrayRoadBaseStructureList);
        rdPavementStructureMap.put("stringArrayRoadSurfaceThicknessList",stringArrayRoadSurfaceThicknessList);
        rdPavementStructureMap.put("stringArrayRoadBaseThicknessList",stringArrayRoadBaseThicknessList);

        return rdPavementStructureMap;
    }
    public AjaxResult insertPavementMaterialsByRoadSectionName(Map<String ,String> data){
        List<RdRoadStake> rdRoadStakes = new ArrayList<>();
        List<RdPavementStructure> rdPavementStructures = new ArrayList<>();
        if (StringUtils.isNotEmpty(data.get("road"))&&!"".equals(data.get("road"))){
            //获取所有路转的序列号和id
            rdRoadStakes = rdRoadStakeMapper.selectRoadPavementMaterialByRoadSectionName(data.get("road"));
        }
        if (StringUtils.isNotEmpty(data.get("roadSurfaceStructureList"))
                &&StringUtils.isNotEmpty(data.get("roadBaseStructureList"))
                &&StringUtils.isNotEmpty(data.get("roadBaseThicknessList"))
                &&StringUtils.isNotEmpty(data.get("roadSurfaceThicknessList"))){
            for (RdRoadStake roadStake:rdRoadStakes) {
                RdPavementStructure rdPavementStructure = new RdPavementStructure();
                rdPavementStructure.preInsert();
                rdPavementStructure.setFkId(roadStake.getRoadSection());
                rdPavementStructure.setSerialNumber(roadStake.getSerialNumber());
                rdPavementStructure.setCombinationOfBaseStructure(data.get("roadBaseStructureList"));
                rdPavementStructure.setCombinationOfSurfaceStructure(data.get("roadSurfaceStructureList"));
                rdPavementStructure.setCombinationOfBaseThickness(data.get("roadBaseThicknessList"));
                rdPavementStructure.setCombinationOfSurfaceThickness(data.get("roadSurfaceThicknessList"));
                rdPavementStructures.add(rdPavementStructure);
            }
            int result = rdPavementStructureMapper.insertRdRoadPavementStructureList(rdPavementStructures);
            if (result==0){
                try {
                    throw new SQLException();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            //修改日志表
            updateRoadImgInfoByRoadSectionid(data.get("road"));
        }
        return AjaxResult.success();
    }
    public int updateRoadImgInfoByRoadSectionid(String id){
        String roadType = sysDictDataMapper.selectDictDataValue("路面结构", "rd_data_type");
        if (StringUtils.isNotEmpty(id)&&!"".equals(id)){
            RdRoadSection byRoadName = sectionMapper.getByRoadName(id);
            RdImportLog rdImportLog = new RdImportLog();
            rdImportLog.setRoadSectionId(byRoadName.getId());
            rdImportLog.setImportType(roadType);
            rdImportLog.setResult(1);
            rdImportLog.setImportState(1);
            rdImportLogMapper.updateByRoadSectionid(rdImportLog);
        }
        return 0;
    }
}
